package cn.quevo.springmvc.spring.boot.handler;

import cn.quevo.core.entity.ResultEntity;
import cn.quevo.core.enums.ResultStatusEnum;
import cn.quevo.core.exception.CustomException;
import cn.quevo.core.exception.NotLoginException;
import cn.quevo.core.exception.UnauthorizedException;
import cn.quevo.core.lang.StringUtils;
import cn.quevo.spring.util.WebUtils;
import cn.quevo.springmvc.spring.boot.autoconfigure.SpringMVCProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

@ControllerAdvice(annotations= {RestController.class,Controller.class})
public class ExceptionHandlerController {
    private final static Logger log = LoggerFactory.getLogger(ExceptionHandlerController.class);
    @Autowired
    private SpringMVCProperties properties;

    /**
     * 未登录异常
     *
     * @param e
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018年1月4日 下午12:50:03
     */
    @ExceptionHandler(NotLoginException.class)
    public Object handleException(NotLoginException e) {
        log.error(e.getMessage(), e);
        if (WebUtils.isAjax(WebUtils.getHttpServletRequest())) {
            return ResponseEntity.status(HttpStatus.OK).body(new ResultEntity(e));
        }
        return WebUtils.toRedirect(StringUtils.isBlank(properties.getLogin()) ? "/login" : properties.getLogin());
    }

    /**
     * 自定义异常
     *
     * @param e
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018年1月4日 下午12:50:03
     */
    @ExceptionHandler(CustomException.class)
    public Object handleException(CustomException e) {
        log.error(e.getMessage(), e);
        if (WebUtils.isAjax(WebUtils.getHttpServletRequest())) {
            return ResponseEntity.status(HttpStatus.OK).body(new ResultEntity(e));
        }
        return WebUtils.toRedirect(StringUtils.isBlank(properties.getLogin()) ? "/500" : properties.getLogin());
    }

    /**
     * 无权限异常
     *
     * @param e
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018年1月4日 下午12:50:03
     */
    @ExceptionHandler(UnauthorizedException.class)
    public Object handleException(UnauthorizedException e) {
        log.error(e.getMessage(), e);
        if (WebUtils.isAjax(WebUtils.getHttpServletRequest())) {
            return ResponseEntity.status(HttpStatus.OK).body(new ResultEntity(e));
        }
        return WebUtils.toRedirect(StringUtils.isBlank(properties.getLogin()) ? "/401" : properties.getLogin());
    }

    /**
     * 全局异常
     *
     * @param e
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018年1月4日 下午12:49:37
     */
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        log.error(e.getMessage(), e);
        if (WebUtils.isAjax(WebUtils.getHttpServletRequest())) {
            return ResponseEntity.status(HttpStatus.OK).body(new ResultEntity(ResultStatusEnum.SERVICE_ERROR));
        }
        return WebUtils.toRedirect(StringUtils.isBlank(properties.getLogin()) ? "/500" : properties.getLogin());
    }
}
